<?php

namespace App\Admin\Controllers;

use App\Guardian;
use App\Http\Controllers\Controller;
use App\User;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Str;

class UserController extends Controller
{
    use ModelForm;

    private $header = '用户管理';
    private $states = [
        'on' => ['value' => 1, 'text' => '禁用', 'color' => 'default'],
        'off' => ['value' => 0, 'text' => '激活', 'color' => 'primary'],
    ];


    public function __construct()
    {
        User::creating(function (User $user) {
            if (!$user->id) {
                $user->id = Str::uuid();
            }
            if (!$user->wasChanged('valid')) {
                $user->valid = 0;
            }
        });
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('新增');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {

//            $grid->id('ID');
            $grid->column('code', '编号')->sortable();
            $grid->column('mobile', '手机号')->editable()->sortable();
//            $grid->id('email')->sortable();
            $grid->column('valid', '状态')->switch($this->states)->sortable();
            $grid->column('name', '名称')->editable();
            $grid->created_at('创建时间');
            $grid->filter(function ($filter) {

                // Sets the range query for the created_at field
                $filter->like('code', '编号');
                $filter->like('mobile', '手机号');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {

            $form->hidden('id', 'ID')->default(Str::uuid());
            $form->text('name', '名称');
            $form->text('code', '编号');
            $form->mobile('mobile', '手机号');
            $form->password('password', '密码');
            $form->switch('valid', '状态')->states($this->states)->default(0);
        });
    }
}
